<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><title>JavaScript 进阶 | XC's Blog</title><meta name="keywords" content="Web APIs笔记"><meta name="author" content="XC's Blog"><meta name="copyright" content="XC's Blog"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="作用域 作用域规定了变量能够被访问的范围，离开这个范围变量就不能被访问  作用域分为： 局部作用域局部作用域分为函数作用域和块作用域。 函数作用域：  在函数内部声明的变量只能在函数内部被访问，外部无法访问  总结：  函数内部声明的变量，在函数外部无法被访问 函数的参数也是函数内部的局部的变量 不同函数内部声明的变量无法相互访问 函数执行完毕后，函数的变量实际被清空了  块作用域：  在Java"><meta property="og:type" content="article"><meta property="og:title" content="JavaScript 进阶"><meta property="og:url" content="https://www.webxc.ml/post/tf35/index.html"><meta property="og:site_name" content="XC&#39;s Blog"><meta property="og:description" content="作用域 作用域规定了变量能够被访问的范围，离开这个范围变量就不能被访问  作用域分为： 局部作用域局部作用域分为函数作用域和块作用域。 函数作用域：  在函数内部声明的变量只能在函数内部被访问，外部无法访问  总结：  函数内部声明的变量，在函数外部无法被访问 函数的参数也是函数内部的局部的变量 不同函数内部声明的变量无法相互访问 函数执行完毕后，函数的变量实际被清空了  块作用域：  在Java"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://npm.elemecdn.com/webxc/cover/js1.webp"><meta property="article:published_time" content="2023-03-12T00:00:00.000Z"><meta property="article:modified_time" content="2023-05-04T17:24:22.255Z"><meta property="article:author" content="XC&#39;s Blog"><meta property="article:tag" content="Web APIs笔记"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://npm.elemecdn.com/webxc/cover/js1.webp"><link rel="shortcut icon" href="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><link rel="canonical" href="https://www.webxc.ml/post/tf35/"><link rel="preconnect" href="//cdn.jsdelivr.net"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://npm.elemecdn.com/@fortawesome/fontawesome-free@6.2.0/css/all.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:void 0,localSearch:{path:"/search.xml",preload:!1,languages:{hits_empty:"找不到您查询的内容：${query}"}},translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlighjs",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"吾辈宣布,你的剪切板已被占领",error:"呀~复制失败,请在尝试下",noSupport:"对不起,你的浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"",date_suffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:{limitCount:60,languages:{author:"作者: XC's Blog",link:"链接: ",source:"来源: XC's Blog",info:"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},lightbox:"fancybox",Snackbar:{chs_to_cht:"您已切换为繁体",cht_to_chs:"您已切换为简体",day_to_night:"您已切换为深色主题",night_to_day:"您已切换为浅色主题",bgLight:"#3b70fc",bgDark:"#121212",position:"bottom-center"},source:{justifiedGallery:{js:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.min.js",css:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.css"}},isPhotoFigcaption:!1,islazyload:!0,isAnchor:!0}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"JavaScript 进阶",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2023-05-04 17:24:22"}</script><noscript><style>#nav{opacity:1}.justified-gallery img{opacity:1}#post-meta time,#recent-posts time{display:inline!important}</style></noscript><script>(e=>{e.saveToLocal={set:function(e,t,o){if(0===o)return;const n=864e5*o,a={value:t,expiry:(new Date).getTime()+n};localStorage.setItem(e,JSON.stringify(a))},get:function(e){const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!((new Date).getTime()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=e=>new Promise(((t,o)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=o,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,t())},document.head.appendChild(n)})),e.activateDarkMode=function(){document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=function(){document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/web-xc/My-blog-CSS/mgi.css"><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/web-xc/My-blog-CSS/mg111.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.16/css/mg2.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styless.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styles.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/font.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.15/css/font2.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiperstyle.css" media="print" onload='this.media="all"'><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">22</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-shouye"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-shijian"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw iconfont icon-fenlei"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-lianxiwomen"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-iconzhengli-"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw iconfont icon-a-wangzhanchaojilianjiehulianwang"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw iconfont icon-wangzhandingzhi"></i> <span>网址导航</span></a></li><li><a class="site-page child" href="https://www.webxc.ml/music/"><i class="fa-fw iconfont icon-musical-note"></i> <span>音乐解锁</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url('https://npm.elemecdn.com/webxc/cover/js1.webp')"><nav id="nav"><span id="blog_name"><div class="back-home-button" tabindex="-1"><i class="back-home-button-icon fas fa-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">项目</div><div class="back-menu-list"><a class="back-menu-item" href="/" title="前往博客主页" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">主站</span></a><a class="back-menu-item" href="https://e.webxc.tk/" rel="external nofollow" title="前往学习笔记" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">学习笔记</span></a><a class="back-menu-item" href="https://v.webxc.tk/" rel="external nofollow" title="VuePress学习文档" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">VuePress文档</span></a><a class="back-menu-item" href="https://b.webxc.tk/" rel="external nofollow" title="旧站Hexo" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">旧站Hexo</span></a><a class="back-menu-item" href="https://gridea.webxc.ml/" rel="external nofollow" title="Gridea笔记" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Gridea笔记</span></a><a class="back-menu-item" href="https://c.webxc.tk/" rel="external nofollow" title="魔改Hexo" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">魔改Hexo</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">博客镜像</div><div class="back-menu-list"><a class="back-menu-item" href="https://blog.webxc.ml/" title="Vercel镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Vercel镜像</span></a><a class="back-menu-item" href="https://webxc.tk/" title="Netlify镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Netlify镜像</span></a><a class="back-menu-item" href="https://web-xc.gitee.io/" title="Gitee镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Gitee镜像</span></a><a class="back-menu-item" href="https://cf.webxc.ml/" title="Cloudflare镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Cloudflare镜像</span></a><a class="back-menu-item" href="https://render.webxc.ml/" title="Render镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Render镜像</span></a></div></div></div></div><a id="site-name" href="/"><div class="title">XC's Blog</div><i class="fa-solid fa-house"></i></a></span><div class="mask-name-container"><center id="name-container"><a id="page-name" href="javascript:btf.scrollToDest(0, 500)">PAGE_NAME</a></center></div><div id="menus"><div class="nav-button" id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i> <span>搜索</span></a></div><div class="nav-button" id="darkmode_navswitch"><a class="darkmode_switchbutton" type="button" title="浅色和深色模式转换" onclick="switchDarkMode()"><i class="fas fa-adjust"></i></a></div><div class="nav-button" id="nav-totop"><a class="totopbtn"><i class="fas fa-arrow-up"></i><span id="percent" onclick="btf.scrollToDest(0,500)">0</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-shouye"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-shijian"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw iconfont icon-fenlei"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-lianxiwomen"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-iconzhengli-"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw iconfont icon-a-wangzhanchaojilianjiehulianwang"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw iconfont icon-wangzhandingzhi"></i> <span>网址导航</span></a></li><li><a class="site-page child" href="https://www.webxc.ml/music/"><i class="fa-fw iconfont icon-musical-note"></i> <span>音乐解锁</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JavaScript 进阶</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-12T00:00:00.000Z" title="发表于 2023-03-12 00:00:00">2023-03-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-04T17:24:22.255Z" title="更新于 2023-05-04 17:24:22">2023-05-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Web-APIs%E7%AC%94%E8%AE%B0/">Web APIs笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span data-flag-title="JavaScript 进阶"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="twikoo_visitors"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h2><blockquote><p>作用域规定了变量能够被访问的范围，离开这个范围变量就不能被访问</p></blockquote><p><strong>作用域分为：</strong></p><h3 id="局部作用域"><a href="#局部作用域" class="headerlink" title="局部作用域"></a>局部作用域</h3><p>局部作用域分为函数作用域和块作用域。</p><p><strong>函数作用域：</strong></p><blockquote><p>在函数内部声明的变量只能在函数内部被访问，外部无法访问</p></blockquote><p>总结：</p><ol><li>函数内部声明的变量，在函数外部无法被访问</li><li>函数的参数也是函数内部的局部的变量</li><li>不同函数内部声明的变量无法相互访问</li><li>函数执行完毕后，函数的变量实际被清空了</li></ol><p><strong>块作用域：</strong></p><blockquote><p>在JavaScript中使用 <code>&#123;&#125;</code> 包裹的代码块内部声明的变量外部将有可能无法被访问</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i=<span class="number">0</span>;i&lt;<span class="number">10</span>;i++)&#123;</span><br><span class="line">    <span class="comment">// i 只能的该代码块中被访问</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(i)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 超出了t的作用域 ，报错</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(i)</span><br></pre></td></tr></table></figure><p><strong>总结：</strong></p><ol><li><code>let</code> 声明的变量，<code>const</code> 声明的常量，都会产生块级作用域</li><li><code>var</code> 声明的变量不会产生块级作用域</li><li>不同代码块之间的变量无法相互访问</li><li>推荐使用 <code>let</code> 和 <code>const</code></li></ol><h3 id="全局作用域"><a href="#全局作用域" class="headerlink" title="全局作用域"></a>全局作用域</h3><blockquote><p>在 <code>&lt;script&gt;</code> 标签 和 <code>.js</code> 文件的<strong>最外层</strong>就是全局作用域，在此声明的变量在函数内部也可以访问，全局作用域下声明的变量，任何其他作用域都可以被访问</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//全局作用域，下声明变量num</span></span><br><span class="line"><span class="keyword">const</span> num=<span class="number">10</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="comment">//函数内部可以使用</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(num)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>注意：</strong></p><ol><li>为 window 对象动态添加的属性默认也是全局的，不推荐</li><li>函数未使用任何关键字声明的变量为全局变量，不推荐</li><li>尽可能少的声明全局变量，防止全局变量被污染</li></ol><h3 id="作用域链"><a href="#作用域链" class="headerlink" title="作用域链"></a>作用域链</h3><blockquote><p>作用域链本质是底层的变量查找机制</p></blockquote><ul><li>函数在被执行时，会优先找当前函数作用域中查找变量</li><li>如果当前作用域查找不到则会依次逐级查找父级作用域直到全局作用域</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//全局作用域</span></span><br><span class="line"><span class="keyword">let</span> a = <span class="number">1</span></span><br><span class="line"><span class="keyword">let</span> b = <span class="number">2</span></span><br><span class="line"><span class="comment">//局部作用域</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">f</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> a = <span class="number">1</span></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">g</span>(<span class="params"></span>) &#123;</span><br><span class="line">        a = <span class="number">2</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(a)  <span class="comment">// 2</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">g</span>()</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">f</span>()</span><br></pre></td></tr></table></figure><p><strong>总结：</strong></p><ol><li>嵌套更新的作用域串联起来形成了作用域链</li><li>相同作用域链中按着从小到大的规则查找变量</li><li>子作用域能够访问父级作用域，父级作用域无法访问子级作用域</li></ol><h3 id="JS-的垃圾回收机制"><a href="#JS-的垃圾回收机制" class="headerlink" title="JS 的垃圾回收机制"></a>JS 的垃圾回收机制</h3><blockquote><p>垃圾回收机制 (Garbage Collection) 简称 GC</p><p>JS 中的内存的分配和回收都是自动完成的，，内存在不使用的时候会被垃圾回收器自动回收。</p><p>但如果不了解JS的内存管理机制，我们同样非常容易成内存泄漏（内存无法被回收）的情况</p><p>不再用到的内存，没有及时释放，就叫做内存泄漏</p></blockquote><p><strong>内存的生命周期 ：</strong></p><ol><li>内存分配：当我们声明变量、函数、对象的时候，系统会自动为他们分配内存</li><li>内存使用：即读写内存，也就是使用变量、函数等</li><li>内存回收：使用完毕，由垃圾回收自动回收不再使用的内存</li><li><ul><li>全局变量一般不会回收(关闭页面回收)；</li><li>一般情况下局部变量的值, 不用了, 会被自动回收掉</li></ul></li></ol><p><strong>垃圾回收算法说明</strong></p><blockquote><p>所谓垃圾回收, 核心思想就是如何判断内存是否已经不再会被使用了, 如果是, 就视为垃圾, 释放掉<br>下面介绍两种常见的浏览器垃圾回收算法: 引用计数法 和 标记清除法</p></blockquote><p>引用计数<br>IE采用的引用计数算法, 定义“内存不再使用”的标准很简单，就是看一个对象是否有指向它的引用。<br>算法：</p><ol><li>跟踪记录每个值被引用的次数。</li><li>如果这个值的被引用了一次，那么就记录次数1</li><li>多次引用会累加。</li><li>如果减少一个引用就减1。</li><li>如果引用次数是0 ，则释放内存。</li></ol><p>引用计数算法是个简单有效的算法。<br>但它却存在一个致命的问题：嵌套引用。<br>如果两个对象相互引用，尽管他们已不再使用，垃圾回收器不会进行回收，导致内存泄露。</p><p>标记清除法<br>现代的浏览器已经不再使用引用计数算法了。<br>现代浏览器通用的大多是基于标记清除算法的某些改进算法，总体思想都是一致的。<br>核心：</p><ol><li>标记清除算法将“不再使用的对象”定义为“无法达到的对象”。</li><li>就是从根部（在JS中就是全局对象）出发定时扫描内存中的对象。 凡是能从根部到达的对象，都是还需要使用的。</li><li>那些无法由根部出发触及到的对象被标记为不再使用，稍后进 行回收。</li></ol><p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220720101238312.png"></p><h3 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h3><blockquote><p>概念：一个函数对周围状态的引用捆绑在一起，内层函数中访问到其外层函数的作用域<br>简单理解：闭包 &#x3D; 内层函数 + 外层函数的变量</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">outer</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> a = <span class="number">1</span></span><br><span class="line">    <span class="keyword">function</span> <span class="title function_">f</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(a)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">f</span>()</span><br><span class="line">&#125;</span><br><span class="line">outer</span><br><span class="line"><span class="comment">//	内层函数调用外层函数的变量，就是闭包</span></span><br></pre></td></tr></table></figure><p>作用： 封闭数据，提供操作，外部也可以访问函数内部的变量</p><p>基本格式：</p><p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220720102143313.png"></p><p>闭包应用：实现数据的私有<br>比如，我们要做个统计函数调用次数，函数调用一次，就++</p><p>普通写法</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> count = <span class="number">1</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    count++</span><br><span class="line">     <span class="variable language_">console</span>.<span class="title function_">log</span>(count)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>() <span class="comment">//2</span></span><br><span class="line"><span class="title function_">fn</span>() <span class="comment">//3</span></span><br></pre></td></tr></table></figure><p>这个count 是个全局变量，很容易被修改</p><p>闭包写法：</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> count = <span class="number">1</span></span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">function</span> <span class="title function_">fun</span>(<span class="params"></span>) &#123;</span><br><span class="line">        count++</span><br><span class="line">       <span class="variable language_">console</span>.<span class="title function_">log</span>(count)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> result =<span class="title function_">fn</span>()</span><br><span class="line"><span class="title function_">result</span>() <span class="comment">//2</span></span><br><span class="line"><span class="title function_">result</span>() <span class="comment">//3</span></span><br></pre></td></tr></table></figure><p>实现了数据私有，无法直接修改count</p><p>闭包可能引起的问题：内存泄漏</p><h3 id="变量提升"><a href="#变量提升" class="headerlink" title="变量提升"></a>变量提升</h3><blockquote><p>变量提升是 JavaScript 中比较“奇怪”的现象，它允许在变量声明之前即被访问（仅存在于var声明变量）</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str) <span class="comment">//不报错 控制台显示undefined</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;hello&#x27;</span></span><br></pre></td></tr></table></figure><p>注意：</p><ol><li>变量在未声明即被访问时会报语法错误</li><li>变量在var声明之前即被访问，变量的值为 undefined</li><li>let &#x2F; const 声明的变量不存在变量提升</li><li>变量提升出现在相同作用域当中</li><li>实际开发中推荐先声明再访问变量</li></ol><h2 id="函数进阶"><a href="#函数进阶" class="headerlink" title="函数进阶"></a>函数进阶</h2><h3 id="函数提升"><a href="#函数提升" class="headerlink" title="函数提升"></a>函数提升</h3><p>函数提升与变量提升比较类似，是指函数在声明之前即可被调用。</p><h3 id="函数参数"><a href="#函数参数" class="headerlink" title="函数参数"></a>函数参数</h3><ul><li><p>动态参数</p><p>arguments 是函数内部内置的伪数组变量，它包含了调用函数时传入的所有实参</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//求和函数 不管用户传入几个实参，都要把和求出来</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> s = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>;i &lt; <span class="variable language_">arguments</span>.<span class="property">length</span>;i++)&#123;</span><br><span class="line">        s += <span class="variable language_">arguments</span>[i]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> s</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">sum</span>(<span class="number">5</span>,<span class="number">10</span>) <span class="comment">//15</span></span><br><span class="line"><span class="title function_">sum</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment">//6</span></span><br></pre></td></tr></table></figure></li><li><p>剩余参数</p><p>剩余参数允许我们将一个不定数量的参数表示为一个数组</p><ol><li><code>...</code> 是语法符号，置于最末函数形参之前，用于获取多余的实参</li><li>借助 <code>...</code> 获取的剩余实参，是个真数组</li></ol><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">...other</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> s = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>;i &lt; other.<span class="property">length</span>;i++)&#123;</span><br><span class="line">        s += other[i]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> s</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">sum</span>(<span class="number">5</span>,<span class="number">10</span>) <span class="comment">//15</span></span><br><span class="line"><span class="title function_">sum</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment">//6</span></span><br></pre></td></tr></table></figure><p>区别：</p><ol><li>… 是语法符号，置于最末函数形参之前，用于获取多余的实参</li><li>借助 … 获取的剩余实参，是个真数组</li></ol></li></ul><hr><p>展开运算符：<code>...</code></p><blockquote><p>展开运算符 (…)， 将一个数组进行展开，不会修改原数组</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(...arr) <span class="comment">//1 2 3 4 5</span></span><br></pre></td></tr></table></figure><p>典型运用场景： 求数组最大值(最小值)、合并数组等</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">6</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"><span class="keyword">const</span> arr2=[<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>]</span><br><span class="line"><span class="comment">//最大值</span></span><br><span class="line"><span class="keyword">const</span> max=<span class="title class_">Math</span>.<span class="title function_">max</span>(...arr)</span><br><span class="line"><span class="comment">//合拼</span></span><br><span class="line"><span class="keyword">const</span> arr3=[...arr,...arr2]</span><br></pre></td></tr></table></figure><p>展开运算符 or 剩余参数<br>剩余参数：函数参数使用，得到真数组<br>展开运算符：数组中使用，数组展开</p><h3 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h3><p><strong>语法：</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//普通函数</span></span><br><span class="line"><span class="keyword">const</span> fn = <span class="keyword">function</span> (<span class="params"></span>)&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>()</span><br><span class="line"><span class="comment">//1.箭头 无参数</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = (<span class="params"></span>) =&gt;&#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//2.只有一个参数</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = x=&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> x</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//3.函数体只有一行代码，可以写一行上，无需写 return 直接返回值</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn</span> = (<span class="params">x,y</span>) =&gt; x + y  <span class="comment">//返回 x + y 的值</span></span><br><span class="line"><span class="comment">//加括号的函数体返回对象字面量表达式</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">fn1</span> = uname =&gt;(&#123;uname : uname&#125;) </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">fn1</span>(<span class="string">&#x27;叶湘伦&#x27;</span>)) <span class="comment">//控制台：&#123;uname:&quot;叶湘伦&quot;&#125;</span></span><br></pre></td></tr></table></figure><ol><li>箭头函数属于表达式函数，因此不存在函数提升</li><li>箭头函数只有一个参数时可以省略圆括号 ()</li><li>箭头函数函数体只有一行代码时可以省略花括号 {}，并自动做为返回值被<br>返回</li><li>加括号的函数体返回对象字面量表达式</li></ol><p><strong>箭头函数参数：</strong></p><ol><li>普通函数有 <code>arguments</code> 动态参数</li><li>箭头函数没有 <code>arguments</code> 动态参数，但是有 剩余参数 <code>..args</code></li></ol><p><strong>箭头函数 this：</strong><br>在箭头函数出现之前，每一个新函数根据它是被如何调用的来定义这个函数的 this 值<br>箭头函数不会创建自己的 this ,它只会从自己的作用域链的上一层沿用 this 。</p><p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220720110228858.png"></p><p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220720110259612.png"></p><p>在开发中【使用箭头函数前需要考虑函数中 this 的值】，事件回调函数使用箭头函数时，this 为全局的 window，因此，DOM事件回调函数为了简便，还是不太推荐使用箭头函数</p><h2 id="数组解构"><a href="#数组解构" class="headerlink" title="数组解构"></a>数组解构</h2><blockquote><p>数组解构是将数组的单元值快速批量赋值给一系列变量的简洁语法。</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 普通的数组</span></span><br><span class="line">  <span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">  <span class="comment">// 批量声明变量 a b c </span></span><br><span class="line">  <span class="comment">// 同时将数组单元值 1 2 3 依次赋值给变量 a b c</span></span><br><span class="line">  <span class="keyword">let</span> [a, b, c] = arr;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// 1</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(b); <span class="comment">// 2</span></span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(c); <span class="comment">// 3</span></span><br></pre></td></tr></table></figure><p>基本语法：</p><ol><li>赋值运算符 <code>=</code> 左侧的 <code>[]</code> 用于批量声明变量，右侧数组的单元值将被赋值给左侧的变量</li><li>变量的顺序对应数组单元值的位置依次进行赋值操作</li><li>变量的数量大于单元值数量时，多余的变量将被赋值为 <code>undefined</code></li><li>变量的数量小于单元值数量时，可以通过 <code>...</code> 获取剩余单元值，但只能置于最末位</li><li>允许初始化变量的默认值，且只有单元值为 <code>undefined</code> 时默认值才会生效</li></ol><p><strong>对象解构</strong></p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 普通对象</span></span><br><span class="line"> <span class="keyword">const</span> user = &#123;</span><br><span class="line">   <span class="attr">name</span>: <span class="string">&#x27;小明&#x27;</span>,</span><br><span class="line">   <span class="attr">age</span>: <span class="number">18</span></span><br><span class="line"> &#125;;</span><br><span class="line"> <span class="comment">// 批量声明变量 name age</span></span><br><span class="line"> <span class="comment">// 同时将数组单元值 小明  18 依次赋值给变量 name  age</span></span><br><span class="line"> <span class="keyword">const</span> &#123;name, age&#125; = user</span><br><span class="line"></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(name) <span class="comment">// 小明</span></span><br><span class="line"> <span class="variable language_">console</span>.<span class="title function_">log</span>(age) <span class="comment">// 18</span></span><br></pre></td></tr></table></figure><p>总结：</p><ol><li>赋值运算符 <code>=</code> 左侧的 <code>&#123;&#125;</code> 用于批量声明变量，右侧对象的属性值将被赋值给左侧的变量</li><li>对象属性的值将被赋值给与属性名相同的变量</li><li>对象中找不到与变量名一致的属性时变量值为 <code>undefined</code></li><li>允许初始化变量的默认值，属性不存在或单元值为 <code>undefined</code> 时默认值才会生效</li></ol><h2 id="遍历数组-forEach-方法"><a href="#遍历数组-forEach-方法" class="headerlink" title="遍历数组 forEach 方法"></a>遍历数组 forEach 方法</h2><blockquote><p>forEach() 方法用于调用数组的每个元素，并将元素传递给回调函数</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">遍历数组.<span class="title function_">forEach</span>(<span class="keyword">function</span> (<span class="params">当前数组元素,索引号</span>)&#123;</span><br><span class="line">    <span class="comment">//函数体</span></span><br><span class="line">&#125;) </span><br></pre></td></tr></table></figure><p>注意：</p><ol><li>forEach 主要是遍历数组</li><li>参数当前数组元素是必须要写的， 索引号可选。</li></ol><h2 id="筛选数组-filter-方法"><a href="#筛选数组-filter-方法" class="headerlink" title="筛选数组 filter 方法"></a>筛选数组 filter 方法</h2><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220720111855654.png" style="zoom:200%"><blockquote><p>filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素</p></blockquote><p>主要使用场景： 筛选数组符合条件的元素，并返回筛选之后元素的新数组</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">遍历数组.<span class="title function_">filter</span>(<span class="keyword">function</span> (<span class="params">当前数组元素,索引号</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> 筛选条件</span><br><span class="line">&#125;) </span><br><span class="line"><span class="comment">//筛选数组大于30的元素</span></span><br><span class="line"><span class="keyword">const</span> arr=[<span class="number">10</span>,<span class="number">26</span>,<span class="number">62</span>,<span class="number">61</span>,<span class="number">56</span>,<span class="number">12</span>,<span class="number">36</span>]</span><br><span class="line"><span class="keyword">const</span> re = arr.<span class="title function_">filter</span>(<span class="keyword">function</span>(<span class="params">item</span>)&#123;</span><br><span class="line">    <span class="keyword">return</span> item &gt; <span class="number">30</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(re) <span class="comment">//[62,61,56,36]</span></span><br></pre></td></tr></table></figure><p>返回值：返回数组，包含了符合条件的所有元素。如果没有符合条件的元素则返回空数组<br>参数：currentValue 必须写， index 可选<br>因为返回新数组，所以不会影响原数组</p><h2 id="对象创建方法"><a href="#对象创建方法" class="headerlink" title="对象创建方法"></a>对象创建方法</h2><ul><li><p>利用字面量创建</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">    <span class="attr">name</span>:<span class="string">&#x27;叶湘伦&#x27;</span>,</span><br><span class="line">    <span class="attr">age</span>:<span class="number">18</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li><li><p>利用 new Object 创建对象</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = <span class="keyword">new</span> <span class="title class_">Object</span>(&#123;</span><br><span class="line">    <span class="attr">name</span>:<span class="string">&#x27;叶湘伦&#x27;</span>,</span><br><span class="line">    <span class="attr">age</span>:<span class="number">18</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li><li><p>利用构造函数创建</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 构造函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Obj</span>(<span class="params">name,age</span>)&#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">age</span> = age</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//创建一个对象</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Stu</span> = <span class="keyword">new</span> <span class="title class_">Obj</span>(<span class="string">&#x27;叶湘伦&#x27;</span>，<span class="number">18</span>)</span><br></pre></td></tr></table></figure><p><strong>注意：</strong></p><ul><li>语法：大写字母开头的函数</li><li>约定：它们的命名以大写字母开头。只能由 “new” 操作符来执行。</li></ul><p><strong>说明：</strong></p><ol><li>使用 new 关键字调用函数的行为被称为实例化</li><li>实例化构造函数时没有参数可以省略（）</li><li>构造函数内部不用写 return ，返回值即为新建的对象</li><li>new Object（） new Date（） 也是实例化构造函数</li></ol><p><strong>实列化执行过程：</strong></p><ol><li>创建一个新空对象</li><li>构造函数 this 指向新对象</li><li>执行构造函数代码，修改 this ，添加新的属性</li><li>返回新的对象</li></ol></li></ul><h3 id="实例对象"><a href="#实例对象" class="headerlink" title="实例对象"></a>实例对象</h3><blockquote><p>通过构造函数创建的对象称为实例对象，实例对象中的属性和方法称为实例成员</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 构造函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Obj</span>(<span class="params">name,age</span>)&#123;</span><br><span class="line">    <span class="comment">//构造函数内部的 this 就是实例对象</span></span><br><span class="line">    <span class="comment">//实例对象中动态添加属性</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">age</span> = age</span><br><span class="line">      <span class="comment">//实例对象中动态添加方法</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">sayHi</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;hi&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//实例化 ，Stu 是实列对象</span></span><br><span class="line"><span class="comment">// Stu 实际就是构造函数内部的 this</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">Stu</span> = <span class="keyword">new</span> <span class="title class_">Obj</span>(<span class="string">&#x27;叶湘伦&#x27;</span>，<span class="number">18</span>)</span><br><span class="line"><span class="comment">//访问实例属性</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Stu</span>.<span class="property">name</span>)</span><br><span class="line"><span class="comment">//调用实列方法</span></span><br><span class="line"><span class="title class_">Stu</span>.<span class="title function_">sayHi</span>()</span><br></pre></td></tr></table></figure><p>说明：</p><ol><li>实例对象的属性和方法即为实例成员</li><li>为构造函数传入参数，动态创建结构相同但值不同的对象</li><li>构造函数创建的实例对象彼此独立互不影响。</li></ol><p><strong>静态成员：</strong></p><blockquote><p>在 JavaScript 中底层函数本质上也是对象类型，因此允许直接为函数动态添加属性或方法，构造函数的属性和方法被称为静态成员。</p></blockquote><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 构造函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Obj</span>(<span class="params">name,age</span>)&#123;</span><br><span class="line">   <span class="comment">//实例成员</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//静态属性</span></span><br><span class="line"><span class="title class_">Obj</span>.<span class="property">eyes</span> = <span class="number">2</span> </span><br><span class="line"><span class="title class_">Obj</span>.<span class="property">arms</span> = <span class="number">2</span></span><br><span class="line"><span class="comment">//静态方法</span></span><br><span class="line"><span class="title class_">Obj</span>.<span class="property">walk</span> = funtion () &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;走路中&#x27;</span>)</span><br><span class="line">    <span class="comment">//this 指向 person</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">eyes</span>) </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>总结：</p><ol><li>静态成员指的是添加到构造函数本身的属性和方法</li><li>一般公共特征的属性或方法静态成员设置为静态成员</li><li>静态成员方法中的 <code>this</code> 指向构造函数本身</li></ol><h2 id="内置构造函数"><a href="#内置构造函数" class="headerlink" title="内置构造函数"></a>内置构造函数</h2><p>在 JavaScript 中<strong>最主要</strong>的数据类型有 6 种，分别是字符串、数值、布尔、undefined、null 和 对象，常见的对象类型数据包括数组和普通对象。其中字符串、数值、布尔、undefined、null 也被称为简单类型或基础类型，对象也被称为引用类型。</p><p>在 JavaScript 内置了一些构造函数，绝大部的数据处理都是基于这些构造函数实现的，JavaScript 基础阶段学习的 <code>Date</code> 就是内置的构造函数。</p><p>引用类型：</p><p><code>Object</code> :</p><p>三个常用静态方法（静态方法就是只有构造函数Object可以调用的）</p><p><code>Object.keys</code> 静态方法获取对象中所有属性（键）</p><p><code>Object.values</code> 静态方法获取对象中所有属性值</p><p><code>Object.assign</code> 静态方法常用于对象拷贝</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Object.keys</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;<span class="attr">name</span>:<span class="string">&#x27;叶湘伦&#x27;</span>,<span class="attr">age</span>:<span class="number">18</span>&#125;</span><br><span class="line"><span class="keyword">const</span> arr = <span class="title class_">Object</span>.<span class="title function_">key</span>(obj)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr)  <span class="comment">// [&#x27;name&#x27;,&#x27;age&#x27;]  返回是一个数组</span></span><br><span class="line"><span class="comment">//Object.values</span></span><br><span class="line"><span class="keyword">const</span> arr2 =<span class="title class_">Object</span>.<span class="title function_">values</span>(obj)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr2)  <span class="comment">// [&#x27;叶湘伦&#x27;,18]  返回是一个数组</span></span><br><span class="line"><span class="comment">//Object.assign</span></span><br><span class="line"><span class="keyword">const</span> obj2 = &#123;&#125;</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">assign</span>(obj2,obj)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj2)  <span class="comment">// &#123;name:&#x27;叶湘伦&#x27;,age:18&#125;</span></span><br><span class="line"><span class="comment">//使用：经常使用的场景给对象添加属性</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">assign</span>(obj,&#123;<span class="attr">sex</span>:<span class="string">&#x27;男&#x27;</span>&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr)  <span class="comment">// &#123;name:&#x27;叶湘伦&#x27;,age:18,sex:&#x27;男&#x27;&#125;</span></span><br></pre></td></tr></table></figure><hr><p><code>Array</code> :</p><p>数组常见实例方法-核心方法</p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220721112028550.png" style="zoom:150%"><table><thead><tr><th>方法</th><th>作用</th><th>说明</th></tr></thead><tbody><tr><td>forEach</td><td>遍历数组</td><td>不返回值，用于不改变值，经常用于查找打印输出值</td></tr><tr><td>filter</td><td>过滤数组</td><td>筛选数组元素，并生成新数组</td></tr><tr><td>map</td><td>迭代数组</td><td>返回新数组，新数组里面的元素是处理之后的值，经常用于处理数据</td></tr><tr><td>reduce</td><td>累积器</td><td>返回函数累计处理的结果，经常用于求和等</td></tr></tbody></table><p>总结：</p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220721115506798.png" style="zoom:150%"><ol><li><p>推荐使用字面量方式声明数组，而不是 <code>Array</code> 构造函数</p></li><li><p>实例方法 <code>forEach</code> 用于遍历数组，替代 <code>for</code> 循环 (重点)</p></li><li><p>实例方法 <code>filter</code> 过滤数组单元值，生成新数组(重点)</p></li><li><p>实例方法 <code>map</code> 迭代原数组，生成新数组(重点)</p></li><li><p>实例方法 <code>join</code> 数组元素拼接为字符串，返回字符串(重点)</p></li><li><p>实例方法 <code>find</code> 查找元素， 返回符合测试条件的第一个数组元素值，如果没有符合条件的则返回 undefined(重点)</p></li><li><p>实例方法<code>every</code> 检测数组所有元素是否都符合指定条件，如果<strong>所有元素</strong>都通过检测返回 true，否则返回 false(重点)</p></li><li><p>实例方法<code>some</code> 检测数组中的元素是否满足指定条件 <strong>如果数组中有</strong>元素满足条件返回 true，否则返回 false</p></li><li><p>实例方法 <code>concat</code> 合并两个数组，返回生成新数组</p></li><li><p>实例方法 <code>sort</code> 对原数组单元值排序</p></li><li><p>实例方法 <code>splice</code> 删除或替换原数组单元</p></li><li><p>实例方法 <code>reverse</code> 反转数组</p></li><li><p>实例方法 <code>findIndex</code> 查找元素的索引值</p></li></ol><p><code>String</code>：</p><p>总结：</p><ol><li>实例属性 <code>length</code> 用来获取字符串的度长(重点)</li><li>实例方法 <code>split(&#39;分隔符&#39;)</code> 用来将字符串拆分成数组(重点)</li><li>实例方法 <code>substring（需要截取的第一个字符的索引[,结束的索引号]）</code> 用于字符串截取(重点)</li><li>实例方法 <code>startsWith(检测字符串[, 检测位置索引号])</code> 检测是否以某字符开头(重点)</li><li>实例方法 <code>includes(搜索的字符串[, 检测位置索引号])</code> 判断一个字符串是否包含在另一个字符串中，根据情况返回 true 或 false(重点)</li><li>实例方法 <code>toUpperCase</code> 用于将字母转换成大写</li><li>实例方法 <code>toLowerCase</code> 用于将就转换成小写</li><li>实例方法 <code>indexOf</code> 检测是否包含某字符</li><li>实例方法 <code>endsWith</code> 检测是否以某字符结尾</li><li>实例方法 <code>replace</code> 用于替换字符串，支持正则匹配</li><li>实例方法 <code>match</code> 用于查找字符串，支持正则匹配</li></ol><p>注：String 也可以当做普通函数使用，这时它的作用是强制转换成字符串数据类型。</p><hr><h2 id="原型"><a href="#原型" class="headerlink" title="原型"></a>原型</h2><blockquote><p>JavaScript 规定，每一个构造函数都有一个 prototype 属性，指向另一个对象，所以我们也称为原型对象</p></blockquote><p><strong>作用：</strong></p><ol><li><p>共享方法</p></li><li><p>可以把那些不变的方法，直接定义在 prototype 对象上</p></li></ol><p>constructor 属性： 指向该原型对象的构造函数</p><p>对象原型：<br>对象都会有一个属性 <code>__proto__</code> 指向构造函数的 prototype 原型对象，之所以我们对象可以使用构造函数 prototype 原型对象的属性和方法，就是因为对象有 <code>__proto__</code> 原型的存在</p><p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220722105529340.png"></p><p><strong>总结：</strong></p><ol><li><code>prototype</code>是什么？哪里来的？</li></ol><ul><li>原型（原型对象）</li><li>构造函数都自动有原型</li></ul><ol start="2"><li><code>constructor</code>属性在哪里？作用干啥的？</li></ol><ul><li><p><code>prototype</code>原型和对象原型<code>__proto__</code>里面都有</p></li><li><p>都指向创建实例对象&#x2F;原型的构造函数</p></li></ul><ol start="3"><li><code>__proto__</code>属性在哪里？指向谁？</li></ol><ul><li>在实例对象里面</li><li>指向原型 <code>prototype</code></li></ul><p>原型继承</p><blockquote><p>继承是面向对象编程的另一个特征，通过继承进一步提升代码封装的程度，JavaScript 中大多是借助原型对象实现继承的特性。</p></blockquote><ol><li><p>封装-抽取公共部分</p><ul><li>把男人和女人公共的部分抽取出来放到人类里面</li></ul></li><li><p>继承-让男人和女人都能继承人类的一些属性和方法</p></li></ol><ul><li>把男人女人公共的属性和方法抽取出来 People</li><li>然后赋值给Man的原型对象，可以共享这些属性和方法</li><li>注意让constructor指回Man这个构造函数</li></ul><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//人类</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">People</span> = &#123;</span><br><span class="line">    <span class="attr">head</span>:<span class="number">1</span>,</span><br><span class="line">    <span class="attr">eyes</span>:<span class="number">2</span>,</span><br><span class="line">    <span class="attr">leys</span>:<span class="number">2</span>,</span><br><span class="line">    <span class="attr">say</span>:<span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//男人</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Man</span>(<span class="params"></span>)&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//公共属性和方法给原型</span></span><br><span class="line"><span class="title class_">Man</span>.<span class="property"><span class="keyword">prototype</span></span> = <span class="title class_">People</span></span><br><span class="line"><span class="comment">//如果我们给男人添加了一个吸烟的方法，发现女人自动也添加这个方法</span></span><br><span class="line"><span class="comment">//男人和女人都同时使用了同一个对象，根据引用类型的特点，他们指向同一个对象，修改一个就会都影响</span></span><br><span class="line"><span class="title class_">Man</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">smoking</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//女人</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Woamn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">//独有方法</span></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">body</span> =<span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 继承写法完善 ,解决上面问题</span></span><br><span class="line"><span class="comment">//男人和女人不要使用同一个对象，但是不同对象里面包含相同的属性和方法</span></span><br><span class="line"><span class="comment">//答案：构造函数</span></span><br><span class="line"><span class="comment">//new 每次都会创建一个新的对象</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">People</span>(<span class="params"></span>) = &#123;</span><br><span class="line">    <span class="attr">head</span>:<span class="number">1</span>,</span><br><span class="line">    <span class="attr">eyes</span>:<span class="number">2</span>,</span><br><span class="line">    <span class="attr">leys</span>:<span class="number">2</span>,</span><br><span class="line">    <span class="attr">say</span>:<span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//男人</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Man</span>(<span class="params"></span>)&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//公共属性和方法</span></span><br><span class="line"><span class="title class_">Man</span>.<span class="property"><span class="keyword">prototype</span></span> =<span class="keyword">new</span> <span class="title class_">People</span>()</span><br></pre></td></tr></table></figure><p><strong>原型链：</strong></p><blockquote><p>基于原型对象的继承使得不同构造函数的原型对象关联在一起，并且这种关联的关系是一种链状的结构，我们将原型对象的链状结构关系称为原型链</p></blockquote><p><img src="" data-lazy-src="https://picgo-fantasy06.oss-cn-guangzhou.aliyuncs.com/img/image-20220722111316754.png"></p><p>原型链-查找规则：</p><ol><li>当访问一个对象的属性（包括方法）时，首先查找这个对象自身有没有该属性。</li><li>如果没有就查找它的原型（也就是 <code>__proto__</code>指向的 <code>prototype</code> 原型对象）</li><li>如果还没有就查找原型对象的原型（<code>Object</code> 的原型对象）</li><li>依此类推一直找到 <code>Object</code> 为止（null）</li><li><code>__proto__</code>对象原型的意义就在于为对象成员查找机制提供一个方向，或者说一条路线</li></ol><hr><h2 id="深浅拷贝"><a href="#深浅拷贝" class="headerlink" title="深浅拷贝"></a>深浅拷贝</h2><h3 id="浅拷贝"><a href="#浅拷贝" class="headerlink" title="浅拷贝"></a>浅拷贝</h3><p><strong>拷贝的是地址</strong><br><strong>常见方法：</strong></p><ol><li>拷贝对象：<code>Object.assgin()</code> 展开运算符 <code>&#123;...obj&#125;</code> 拷贝对象</li><li>拷贝数组：<code>Array.prototype.concat()</code> 或者 <code>[...arr]</code></li></ol><p><strong>总结：</strong></p><ol><li>直接赋值和浅拷贝有什么区别？</li></ol><ul><li>直接赋值的方法，只要是对象，都会相互影响，因为是直接拷贝对<br>象栈里面的地址</li><li>浅拷贝如果是一层对象，不相互影响，如果出现多层对象拷贝还会<br>相互影响</li></ul><ol start="2"><li>浅拷贝怎么理解？</li></ol><ul><li>拷贝对象之后，里面的属性值是简单数据类型直接拷贝值</li><li>如果属性值是引用数据类型则拷贝的是地址</li></ul><h3 id="深拷贝"><a href="#深拷贝" class="headerlink" title="深拷贝"></a>深拷贝</h3><p><strong>拷贝的是对象，不是地址</strong></p><p>常见方法：</p><ol><li>通过递归实现深拷贝</li><li>lodash&#x2F;cloneDeep</li><li>通过JSON.stringify()实现</li></ol><h2 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h2><h3 id="throw-抛异常"><a href="#throw-抛异常" class="headerlink" title="throw 抛异常"></a>throw 抛异常</h3><p>总结：</p><ol><li><code>throw</code> 抛出异常信息，程序也会终止执行</li><li><code>throw</code> 后面跟的是错误提示信息</li><li><code>Error</code> 对象配合 <code>throw</code> 使用，能够设置更详细的错误信息</li></ol><h3 id="try-x2F-catch"><a href="#try-x2F-catch" class="headerlink" title="try&#x2F;catch"></a>try&#x2F;catch</h3><p>总结：</p><ol><li><code>try...catch</code> 用于捕获错误信息</li><li>将预估可能发生错误的代码写在 <code>try</code> 代码段中</li><li>如果 <code>try</code> 代码段中出现错误后，会执行 <code>catch</code> 代码段，并截获到错误信息</li><li><code>finally</code> 不管是否有错误，都会执行</li></ol><p><strong>debugger</strong> ：类似浏览器调试打断点</p><h2 id="处理this"><a href="#处理this" class="headerlink" title="处理this"></a>处理this</h2><h3 id="this指向-普通函数"><a href="#this指向-普通函数" class="headerlink" title="this指向-普通函数"></a>this指向-普通函数</h3><blockquote><p>普通函数的调用方式决定了 this 的值，即【谁调用 this 的值指向谁】</p></blockquote><p>普通函数没有明确调用者时 this 值为 window，严格模式下没有调用者时 this 的值为 undefined</p><h3 id="this指向-箭头函数"><a href="#this指向-箭头函数" class="headerlink" title="this指向-箭头函数"></a>this指向-箭头函数</h3><p>箭头函数中的 this 与普通函数完全不同，也不受调用方式的影响，事实上箭头函数中并不存在 this ！</p><ol><li>箭头函数会默认帮我们绑定外层 this 的值，所以在箭头函数中 this 的值和外层的 this 是一样的</li><li>箭头函数中的this引用的就是最近作用域中的this</li><li>向外层作用域中，一层一层查找this，直到有this的定义</li></ol><h3 id="改变this"><a href="#改变this" class="headerlink" title="改变this"></a>改变this</h3><p><code>call()</code> 方法调用函数，同时指定被调用函数中 this 的值</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fn.<span class="title function_">call</span>(thisArg,arg1,arg2,...)</span><br></pre></td></tr></table></figure><p>说明：</p><ol><li><p><code>thisArg</code>：在 fn 函数运行时指定的 <code>this</code> 值</p></li><li><p><code>arg1，arg2</code>：传递的其他参数</p></li><li><p>返回值就是函数的返回值，因为它就是调用函数</p></li></ol><hr><p><code>apply()</code> 调用函数，同时指定被调用函数中 this 的值</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fn.<span class="title function_">apply</span>(thisArg,[argArray])</span><br></pre></td></tr></table></figure><p>说明：</p><ol><li><code>thisArg</code>：在fn函数运行时指定的 this 值</li><li><code>argsArray</code>：传递的值，必须包含在数组里面</li><li>返回值就是函数的返回值，因为它就是调用函数</li><li>因此 <code>apply</code> 主要跟数组有关系，比如使用 <code>Math.max()</code> 求数组的最大值</li></ol><hr><p><code>bind()</code> 不会调用函数。但是能改变函数内部this 指向</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fn.<span class="title function_">bind</span>(thisArg,arg1,arg2,...)</span><br></pre></td></tr></table></figure><p>说明：</p><ol><li><code>thisArg</code>：在 fn 函数运行时指定的 <code>this</code> 值</li><li><code>arg1，arg2</code>：传递的其他参数</li><li>返回由指定的 this 值和初始化参数改造的 原函数拷贝 （新函数）</li><li>因此当我们只是想改变 this 指向，并且不想调用这个函数的时候，可以使用 bind，比如改变定时器内部的 this 指向.</li></ol><hr><p>总结：</p><ul><li><p>区别</p><ol><li><code>call</code> 和 <code>apply</code> 会调用函数, 并且改变函数内部 this 指向</li><li><code>call</code> 和 <code>apply</code> 传递的参数不一样, <code>call</code> 传递参数 <code>aru1, aru2..</code> 形式 <code>apply</code> 必须数组形式<code>[arg]</code></li><li><code>bind</code> 不会调用函数, 可以改变函数内部 this 指向</li></ol></li><li><p>主要应用场景</p><ol><li>call 调用函数并且可以传递参数</li><li>apply 经常跟数组有关系. 比如借助于数学对象实现数组最大值最小值</li><li>bind 不调用函数,但是还想改变this指向. 比如改变定时器内部的this指向.</li></ol></li></ul><hr><h2 id="节流和防抖"><a href="#节流和防抖" class="headerlink" title="节流和防抖"></a>节流和防抖</h2><p><strong>节流：</strong></p><blockquote><p>就是指连续触发事件但是在 n 秒中只执行一次函数，比如可以利用节流实现 1s之内 只能触发一次鼠标移动事件</p></blockquote><p><strong>防抖：</strong></p><blockquote><p>指触发事件后在 n 秒内函数只能执行一次，如果在 n 秒内又触发了事件，则会重新计算函数执行时间</p></blockquote><p><strong>使用场景</strong></p><ul><li>节流： 鼠标移动，页面尺寸发生变化，滚动条滚动等开销比较大的情况下</li><li>防抖： 搜索框输入，设定每次输入完毕n秒后发送请求，如果期间还有输入，则从新计算时间</li></ul><hr><h1 id="网站推荐"><a href="#网站推荐" class="headerlink" title="网站推荐"></a>网站推荐</h1><hr><h2 id="常用网站"><a href="#常用网站" class="headerlink" title="常用网站"></a>常用网站</h2><ul><li><a target="_blank" rel="noopener" href="https://gitee.com/">Gitee</a></li><li><a target="_blank" rel="noopener" href="https://github.com/">Github</a></li><li><a target="_blank" rel="noopener" href="https://www.nowcoder.com/">NOWCODER</a></li><li><a target="_blank" rel="noopener" href="https://leetcode-cn.com/">LeetCode</a></li><li><a target="_blank" rel="noopener" href="https://codetop.cc/home">CodeTop</a></li><li><a target="_blank" rel="noopener" href="https://www.wondercv.com/">WonderCV</a></li><li><a target="_blank" rel="noopener" href="https://mail.qq.com/">QQMail</a></li><li><a target="_blank" rel="noopener" href="https://email.163.com/">NetEase Mail</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/">bilibili</a></li><li><a target="_blank" rel="noopener" href="https://www.zhihu.com/">知乎</a></li><li><a target="_blank" rel="noopener" href="https://36kr.com/">36 氪</a></li><li><a target="_blank" rel="noopener" href="https://juejin.cn/">掘金</a></li><li><a target="_blank" rel="noopener" href="https://www.jianshu.com/">简书</a></li><li><a target="_blank" rel="noopener" href="https://www.feishu.cn/">飞书</a></li><li><a target="_blank" rel="noopener" href="https://shimo.im/">石墨文档</a></li><li><a target="_blank" rel="noopener" href="https://weread.qq.com/">微信读书</a></li><li><a target="_blank" rel="noopener" href="https://haoma.baidu.com/">百度号码平台</a></li><li><a target="_blank" rel="noopener" href="https://fanyi.baidu.com/">百度翻译</a></li><li><a target="_blank" rel="noopener" href="https://www.baidu.com/">百度一下</a></li></ul><h2 id="我爱官网"><a href="#我爱官网" class="headerlink" title="我爱官网"></a>我爱官网</h2><ul><li><a target="_blank" rel="noopener" href="https://docschina.org/">印记中文-前端中文文档集合</a></li><li><a target="_blank" rel="noopener" href="https://cn.vuejs.org/">Vue.js</a></li><li><a target="_blank" rel="noopener" href="https://router.vuejs.org/zh/">Vue Router</a></li><li><a target="_blank" rel="noopener" href="https://vant-contrib.gitee.io/vant/##/zh-CN/home">Vant</a></li><li><a target="_blank" rel="noopener" href="https://nutui.jd.com/#/">Nut UI</a></li><li><a target="_blank" rel="noopener" href="https://didi.github.io/cube-ui/#/zh-CN">Cube UI</a></li><li><a target="_blank" rel="noopener" href="https://element.eleme.cn/#/zh-CN">Element UI</a></li><li><a target="_blank" rel="noopener" href="https://www.iviewui.com/">IView UI</a></li><li><a target="_blank" rel="noopener" href="https://reactjs.org/">React</a></li><li><a target="_blank" rel="noopener" href="https://ant.design/index-cn">Ant Design</a></li><li><a target="_blank" rel="noopener" href="https://redux.js.org/">Redux</a></li><li><a target="_blank" rel="noopener" href="https://www.redux.org.cn/">Redux 中文文档</a></li><li><a target="_blank" rel="noopener" href="http://nodejs.cn/">Node.js 中文网</a></li><li><a target="_blank" rel="noopener" href="https://www.npmjs.com/">npm</a></li><li><a target="_blank" rel="noopener" href="http://www.axios-js.com/">axios 中文文档</a></li><li><a target="_blank" rel="noopener" href="https://www.lodashjs.com/">Lodash 中文文档</a></li><li><a target="_blank" rel="noopener" href="https://www.swiper.com.cn/">Swiper 中文网</a></li><li><a target="_blank" rel="noopener" href="https://echarts.apache.org/zh/index.html">Echarts</a></li><li><a target="_blank" rel="noopener" href="https://less.bootcss.com/">Less</a></li><li><a target="_blank" rel="noopener" href="https://www.bootcss.com/">Bootstrap 中文网</a></li><li><a target="_blank" rel="noopener" href="https://www.jquery123.com/">jQuery API 中文文档</a></li></ul><h2 id="我爱工具"><a href="#我爱工具" class="headerlink" title="我爱工具"></a>我爱工具</h2><ul><li><a target="_blank" rel="noopener" href="https://docsify.js.org/##/">docsify</a></li><li><a target="_blank" rel="noopener" href="https://www.kancloud.cn/">看云</a></li><li><a target="_blank" rel="noopener" href="https://www.bootcdn.cn/">BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务</a></li><li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/jpfss/p/10457026.html">各大开源软件镜像源</a></li><li><a target="_blank" rel="noopener" href="http://koala-app.com/index-zh.html">Koala-Less 编译工具</a></li><li><a target="_blank" rel="noopener" href="https://caniuse.com/">Can I use ?</a></li><li><a target="_blank" rel="noopener" href="https://tool.oschina.net/">OSCHINA 实用在线工具</a></li><li><a target="_blank" rel="noopener" href="https://www.apipost.cn/">ApiPost</a></li><li><a target="_blank" rel="noopener" href="https://www.showdoc.com.cn/">ShowDoc</a></li><li><a target="_blank" rel="noopener" href="http://rap2.taobao.org/account/login">RAP 接口管理平台</a></li><li><a target="_blank" rel="noopener" href="https://www.postman.com/">Postman</a></li><li><a target="_blank" rel="noopener" href="https://jsbin.com/?html,output">JS Bin</a></li><li><a target="_blank" rel="noopener" href="https://www.iconfont.cn/">iconfont</a></li><li><a target="_blank" rel="noopener" href="https://icomoon.io/">IcoMoon</a></li><li><a target="_blank" rel="noopener" href="https://www.bitbug.net/">在线制作 ico 图标</a></li></ul><h2 id="我爱学习"><a href="#我爱学习" class="headerlink" title="我爱学习"></a>我爱学习</h2><ul><li><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/">ES6 入门教程 - ECMAScript 6 入门（阮一峰）</a></li><li><a target="_blank" rel="noopener" href="https://wangdoc.com/javascript/index.html">JavaScript 教程（阮一峰）</a></li><li><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/">MDN 文档</a></li><li><a target="_blank" rel="noopener" href="https://www.ruanyifeng.com/blog/">阮一峰的网络日志</a></li><li><a target="_blank" rel="noopener" href="https://www.liaoxuefeng.com/">廖雪峰的官方网站</a></li><li><a target="_blank" rel="noopener" href="https://tech.meituan.com/">美团技术团队</a></li><li><a target="_blank" rel="noopener" href="https://cnodejs.org/">CNode：Node.js 专业中文社区</a></li><li><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/index.html">W3school</a></li><li><a target="_blank" rel="noopener" href="https://www.w3cschool.cn/">w3cschool</a></li><li><a target="_blank" rel="noopener" href="https://www.runoob.com/">菜鸟教程</a></li><li><a target="_blank" rel="noopener" href="https://www.kancloud.cn/pillys/qianduan/2049475">前端面试宝典-看云文档</a></li></ul><h2 id="我爱项目"><a href="#我爱项目" class="headerlink" title="我爱项目"></a>我爱项目</h2><ul><li><a target="_blank" rel="noopener" href="http://doc.toutiao.liulongbin.top/">黑马头条-移动端</a></li></ul><h2 id="我爱视频"><a href="#我爱视频" class="headerlink" title="我爱视频"></a>我爱视频</h2><ul><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV14J4114768">黑马 pink 前端教程</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1ux411d75J">黑马 JavaScript 基础</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1k4411w7sV">黑马 JS DOM BOM 教程</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1a4411w7Gx">黑马 jQuery</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1zz411q7j3">黑马 ES6 高级语法</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV12J411m7MG">黑马 4 小时上手 Vue</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1RE411W776">黑马 Vue 音乐播放器</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1zq4y1p7ga">黑马 Vue2+Vue3 全套</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1x64y1S7S7">黑马 Vue 全家桶实战</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV14y4y1g7M4">黑马 React</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Ns411N7HU">黑马 Node</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1uK411H7on">尚硅谷 ES6 教程</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1WC4y1b78y">尚硅谷 3 小时入门 Ajax</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1vy4y1s7k6">尚硅谷 5 小时打通 Git</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1e7411j7T5">尚硅谷 Webpack</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1bs411E7pD">尚硅谷 Node</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Zy4y1K7SH">尚硅谷 Vue2+Vue3 全套</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Vf4y1T7bw">尚硅谷 Vue 项目：尚品汇</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1wy4y1D7JT">尚硅谷 React</a></li><li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV15741177Eh">coderwhy Vue</a></li></ul><h2 id="我爱杂七杂八棒棒"><a href="#我爱杂七杂八棒棒" class="headerlink" title="我爱杂七杂八棒棒"></a>我爱杂七杂八棒棒</h2><ul><li><a target="_blank" rel="noopener" href="https://adzhp.cn/">爱达杂货铺</a></li><li><a target="_blank" rel="noopener" href="https://search.chongbuluo.com/">虫部落</a></li><li><a target="_blank" rel="noopener" href="https://vip.bljiex.cc/">VIP 视频在线解析</a></li><li><a target="_blank" rel="noopener" href="https://www.aigei.com/">爱给网-音乐配乐-3D 模型-视频素材-免费下载</a></li><li><a target="_blank" rel="noopener" href="https://www.ear0.com/">耳聆网-音效素材库</a></li><li><a target="_blank" rel="noopener" href="http://c.dituhui.com/">大众制图</a></li><li><a target="_blank" rel="noopener" href="https://sso.tubiaoxiu.com/login">图表秀</a></li><li><a target="_blank" rel="noopener" href="https://www.weiciyun.com/">微词云</a></li><li><a target="_blank" rel="noopener" href="https://www.allhistory.com/">全历史</a></li><li><a target="_blank" rel="noopener" href="https://topbook.cc/overview">Topbook</a></li><li><a target="_blank" rel="noopener" href="https://www.logosc.cn/">LOGO 神器</a></li></ul></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">本文作者:</span> <span class="post-copyright-info"><a href="https://www.webxc.ml">XC's Blog</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">本文链接:</span> <span class="post-copyright-info"><a href="https://www.webxc.ml/post/tf35/">https://www.webxc.ml/post/tf35/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明:</span> <span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.webxc.ml" target="_blank">XC's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Web-APIs%E7%AC%94%E8%AE%B0/">Web APIs笔记</a></div><div class="post_share"><div class="social-share" data-image="https://npm.elemecdn.com/webxc/cover/js1.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" alt="Wechat"></a><div class="post-qr-code-desc">Wechat</div></li><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" alt="Alipay"></a><div class="post-qr-code-desc">Alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/post/fd38/"><img class="prev-cover" src="https://npm.elemecdn.com/webxc/cover/js1.webp" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇文章</div><div class="prev_info">JavaScript 第五天</div></div></a></div><div class="next-post pull-right"><a href="/post/fa31/"><img class="next-cover" src="https://npm.elemecdn.com/webxc/cover/js1.webp" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇文章</div><div class="next_info">ES6模块化与异步编程</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/post/fa31/" title="ES6模块化与异步编程"><img class="cover" src="https://npm.elemecdn.com/webxc/cover/js1.webp" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">ES6模块化与异步编程</div></div></a></div><div><a href="/post/ce68/" title="Web APIs第一天"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/webapis.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-15</div><div class="title">Web APIs第一天</div></div></a></div><div><a href="/post/ar34/" title="Web APIs第七天"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/webapis.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Web APIs第七天</div></div></a></div></div></div><hr><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i> <span>评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="author-info__name">XC's Blog</div><div class="author-info__description">XC's Blog 记录生活和学习文章</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">74</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">22</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://tenapi.cn/qqzlk/?qq=2848141597" target="_blank" title="QQ"><i class="iconfont icon-QQ"></i></a><a class="social-icon" href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.jpg" target="_blank" title="微信"><i class="iconfont icon-weixin"></i></a><a class="social-icon" href="https://github.com/web-xc" target="_blank" title="Github"><i class="iconfont icon-github"></i></a><a class="social-icon" href="https://space.bilibili.com/2134826455" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=323682437" target="_blank" title="网易云音乐"><i class="iconfont icon-wyy"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.</span> <span class="toc-text">作用域</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%80%E9%83%A8%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.1.</span> <span class="toc-text">局部作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.2.</span> <span class="toc-text">全局作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F%E9%93%BE"><span class="toc-number">1.3.</span> <span class="toc-text">作用域链</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#JS-%E7%9A%84%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6"><span class="toc-number">1.4.</span> <span class="toc-text">JS 的垃圾回收机制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%97%AD%E5%8C%85"><span class="toc-number">1.5.</span> <span class="toc-text">闭包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E6%8F%90%E5%8D%87"><span class="toc-number">1.6.</span> <span class="toc-text">变量提升</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E8%BF%9B%E9%98%B6"><span class="toc-number">2.</span> <span class="toc-text">函数进阶</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E6%8F%90%E5%8D%87"><span class="toc-number">2.1.</span> <span class="toc-text">函数提升</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0"><span class="toc-number">2.2.</span> <span class="toc-text">函数参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="toc-number">2.3.</span> <span class="toc-text">箭头函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E8%A7%A3%E6%9E%84"><span class="toc-number">3.</span> <span class="toc-text">数组解构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84-forEach-%E6%96%B9%E6%B3%95"><span class="toc-number">4.</span> <span class="toc-text">遍历数组 forEach 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AD%9B%E9%80%89%E6%95%B0%E7%BB%84-filter-%E6%96%B9%E6%B3%95"><span class="toc-number">5.</span> <span class="toc-text">筛选数组 filter 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E6%96%B9%E6%B3%95"><span class="toc-number">6.</span> <span class="toc-text">对象创建方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1"><span class="toc-number">6.1.</span> <span class="toc-text">实例对象</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">7.</span> <span class="toc-text">内置构造函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8E%9F%E5%9E%8B"><span class="toc-number">8.</span> <span class="toc-text">原型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%B1%E6%B5%85%E6%8B%B7%E8%B4%9D"><span class="toc-number">9.</span> <span class="toc-text">深浅拷贝</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%85%E6%8B%B7%E8%B4%9D"><span class="toc-number">9.1.</span> <span class="toc-text">浅拷贝</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%B1%E6%8B%B7%E8%B4%9D"><span class="toc-number">9.2.</span> <span class="toc-text">深拷贝</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="toc-number">10.</span> <span class="toc-text">异常处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#throw-%E6%8A%9B%E5%BC%82%E5%B8%B8"><span class="toc-number">10.1.</span> <span class="toc-text">throw 抛异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#try-x2F-catch"><span class="toc-number">10.2.</span> <span class="toc-text">try&#x2F;catch</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%84%E7%90%86this"><span class="toc-number">11.</span> <span class="toc-text">处理this</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#this%E6%8C%87%E5%90%91-%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0"><span class="toc-number">11.1.</span> <span class="toc-text">this指向-普通函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#this%E6%8C%87%E5%90%91-%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="toc-number">11.2.</span> <span class="toc-text">this指向-箭头函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%94%B9%E5%8F%98this"><span class="toc-number">11.3.</span> <span class="toc-text">改变this</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8A%82%E6%B5%81%E5%92%8C%E9%98%B2%E6%8A%96"><span class="toc-number">12.</span> <span class="toc-text">节流和防抖</span></a></li></ol><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BD%91%E7%AB%99%E6%8E%A8%E8%8D%90"><span class="toc-number"></span> <span class="toc-text">网站推荐</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E7%BD%91%E7%AB%99"><span class="toc-number">1.</span> <span class="toc-text">常用网站</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%88%B1%E5%AE%98%E7%BD%91"><span class="toc-number">2.</span> <span class="toc-text">我爱官网</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%88%B1%E5%B7%A5%E5%85%B7"><span class="toc-number">3.</span> <span class="toc-text">我爱工具</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%88%B1%E5%AD%A6%E4%B9%A0"><span class="toc-number">4.</span> <span class="toc-text">我爱学习</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%88%B1%E9%A1%B9%E7%9B%AE"><span class="toc-number">5.</span> <span class="toc-text">我爱项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%88%B1%E8%A7%86%E9%A2%91"><span class="toc-number">6.</span> <span class="toc-text">我爱视频</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%88%B1%E6%9D%82%E4%B8%83%E6%9D%82%E5%85%AB%E6%A3%92%E6%A3%92"><span class="toc-number">7.</span> <span class="toc-text">我爱杂七杂八棒棒</span></a></li></ol></li></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/post/us87/" title="JS进阶第二天"><img src="https://npm.elemecdn.com/webxc/cover/js1.webp" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="JS进阶第二天"></a><div class="content"><a class="title" href="/post/us87/" title="JS进阶第二天">JS进阶第二天</a><time datetime="2023-05-05T01:20:00.000Z" title="发表于 2023-05-05 01:20:00">2023-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/re67/" title="JS进阶第一天"><img src="https://npm.elemecdn.com/webxc/cover/js1.webp" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="JS进阶第一天"></a><div class="content"><a class="title" href="/post/re67/" title="JS进阶第一天">JS进阶第一天</a><time datetime="2023-04-26T16:30:00.000Z" title="发表于 2023-04-26 16:30:00">2023-04-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/ar34/" title="Web APIs第七天"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/webapis.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="Web APIs第七天"></a><div class="content"><a class="title" href="/post/ar34/" title="Web APIs第七天">Web APIs第七天</a><time datetime="2023-04-17T20:00:00.000Z" title="发表于 2023-04-17 20:00:00">2023-04-17</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2023 By XC's Blog</div><div class="framework-info"><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/?keyword=20235221">萌ICP备20235221号</a><span class="footer-separator">|</span><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/">萌ICP查询</a><div id="running-time"></div></div><script>setInterval((()=>{let e=Math.round(new Date("2021-11-21 01:35:06").getTime()/1e3),t=Math.round(((new Date).getTime()+288e5)/1e3)-e,n=new Array(0,0,0,0,0);t>=31536e3&&(n[0]=parseInt(t/31536e3),t%=31536e3),t>=86400&&(n[1]=parseInt(t/86400),t%=86400),t>=3600&&(n[2]=parseInt(t/3600),t%=3600),t>=60&&(n[3]=parseInt(t/60),t%=60),t>0&&(n[4]=t),currentTimeHtml="本站已安全运行 "+n[0]+" 年 "+n[1]+" 天 "+n[2]+" 时 "+n[3]+" 分 "+n[4]+" 秒",document.getElementById("running-time").innerHTML=currentTimeHtml}),1e3)</script></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i><span id="percent">0<span>%</span></span></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight,500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i> <span>正在载入文件,请稍后</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"></div></div><hr><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/utils.js"></script><script src="https://npm.elemecdn.com/hexo-theme-butterfly@4.4.0/source/js/main.js"></script><script src="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://npm.elemecdn.com/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.js"></script><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{const e=document.querySelectorAll("#article-container .mermaid-wrap");if(e.length){window.runMermaid=()=>{window.loadMermaid=!0;const t="dark"===document.documentElement.getAttribute("data-theme")?"dark":"default";Array.from(e).forEach(((e,n)=>{const d=e.firstElementChild,r="mermaid-"+n,m="%%{init:{ 'theme':'"+t+"'}}%%\n"+d.textContent;mermaid.mermaidAPI.render(r,m,(e=>{d.insertAdjacentHTML("afterend",e)}))}))};const t=()=>{window.loadMermaid?runMermaid():getScript("https://npm.elemecdn.com/mermaid/dist/mermaid.min.js").then(runMermaid)};window.pjax?t():document.addEventListener("DOMContentLoaded",t)}})()</script><script>(()=>{const o=()=>{twikoo.init(Object.assign({el:"#twikoo-wrap",envId:"https://blog2.webxc.ml/",region:"",onCommentLoaded:function(){btf.loadLightbox(document.querySelectorAll("#twikoo .tk-content img:not(.tk-owo-emotion)"))}},null))},t=()=>{"object"!=typeof twikoo?getScript("https://npm.elemecdn.com/twikoo@1.6.10/dist/twikoo.all.min.js").then(o):setTimeout(o,0)};t()})()</script></div><script data-pjax="true" defer src="https://npm.elemecdn.com/webxc@1.0.16/css/style.js"></script><script defer src="https://npm.elemecdn.com/hexo-theme-butterfly@4.4.0/source/js/search/local-search.js"></scrip><script src="https://npm.elemecdn.com/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!0,POWERMODE.mobile=!0,document.body.addEventListener("input",POWERMODE)</script><script src="https://npm.elemecdn.com/pjax@0.2.8/pjax.min.js"></script><script>let pjaxSelectors=["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax",".tk-comments-container"];var pjax=new Pjax({elements:'a:not([target="_blank"])',selectors:pjaxSelectors,cacheBust:!1,analytics:!1,scrollRestoration:!1});document.addEventListener("pjax:send",(function(){if(window.tocScrollFn&&window.removeEventListener("scroll",window.tocScrollFn),window.scrollCollect&&window.removeEventListener("scroll",scrollCollect),"object"==typeof preloader&&preloader.initLoading(),document.getElementById("rightside").style.cssText="opacity: ''; transform: ''",window.aplayers)for(let e=0;e<window.aplayers.length;e++)window.aplayers[e].options.fixed||window.aplayers[e].destroy();"object"==typeof typed&&typed.destroy();const e=document.body.classList;e.contains("read-mode")&&e.remove("read-mode"),"object"==typeof disqusjs&&disqusjs.destroy()})),document.addEventListener("pjax:complete",(function(){window.refreshFn(),document.querySelectorAll("script[data-pjax]").forEach((e=>{const t=document.createElement("script"),o=e.text||e.textContent||e.innerHTML||"";Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),t.appendChild(document.createTextNode(o)),e.parentNode.replaceChild(t,e)})),GLOBAL_CONFIG.islazyload&&window.lazyLoadInstance.update(),"function"==typeof chatBtnFn&&chatBtnFn(),"function"==typeof panguInit&&panguInit(),"function"==typeof gtag&&gtag("config","",{page_path:window.location.pathname}),"object"==typeof _hmt&&_hmt.push(["_trackPageview",window.location.pathname]),"function"==typeof loadMeting&&document.getElementsByClassName("aplayer").length&&loadMeting(),"object"==typeof Prism&&Prism.highlightAll(),"object"==typeof preloader&&preloader.endLoading()})),document.addEventListener("pjax:error",(e=>{404===e.request.status&&pjax.loadUrl("/404.html")}))</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script data-pjax>function butterfly_swiper_injector_config(){var s=document.getElementById("recent-posts");console.log("已挂载butterfly_swiper"),s.insertAdjacentHTML("afterbegin",'<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/webapis.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-04-11</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fr67/&quot;);" href="javascript:void(0);" alt="">Web APIs第六天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-02-16</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fd38/&quot;);" href="javascript:void(0);" alt="">JavaScript 第五天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-02-07</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/7f86/&quot;);" href="javascript:void(0);" alt="">JavaScript 第四天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-12-27</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/6e+47/&quot;);" href="javascript:void(0);" alt="">JavaScript 第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover15.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-10-31</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/2db5/&quot;);" href="javascript:void(0);" alt="">移动开发-Flex布局</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover16.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-11-14</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/1c7e/&quot;);" href="javascript:void(0);" alt="">移动开发-媒体查询</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover17.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-11-17</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/7fc/&quot;);" href="javascript:void(0);" alt="">移动开发-响应式</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.11/cover/css5.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-10-16</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/d97/&quot;);" href="javascript:void(0);" alt="">CSS-2D-3D转换</a><div class="blog-slider__text">点击查看</div></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div><div class="swiper-button-prev"></div><div class="swiper-button-next"></div></div></div>')}for(var elist="null".split(","),cpage=location.pathname,epage="all",flag=0,i=0;i<elist.length;i++)cpage.includes(elist[i])&&flag++;("all"===epage&&0==flag||epage===cpage)&&butterfly_swiper_injector_config()</script><script defer src="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper_init.js"></script></body></html>